define(["jquery","bbcare"],function($,$bb){
	var servers = {};
	var URL = $bb.baseUrl("followapp");

	/**
	 * 处理基本信息
	 */
	servers.handleBaseInfo = function(info){
		info.testDate = $bb.simple.getCurTime().date;
		var diff = $bb.simple.dateDiff(info.birthday,info.testDate).days;
		var pregnanceDay = info.pregnanceDay ? info.pregnanceDay : 280;
        var flag, days; //flag == 1,实际月龄；flag == 2 ,纠正月龄；
        if( diff < 2*365 && pregnanceDay < 37*7 ){
            info.flag = 2;
            days = diff - ( 40*7 - pregnanceDay );
        }else{
            info.flag = 1;
            days = diff;
        }
        var monthAge = parseInt( days / 30 ) > 0 ? parseInt( days / 30  ) + "月龄" : '';
        monthAge += parseInt( days % 30 ) > 0 ? parseInt( days % 30 ) + "天" : '';
        info.monthAge = monthAge;
		return info;
	}

	/**
	 * 处理问卷数据
	 */
	servers.handleQuestion = function(data,result){
		var arr = new Array(5);
		for(var key in data){
			var obj = {};
			obj.name = key;
			obj.question = data[key];
			obj.ids = [];
			for (var i = 0; i < obj.question.length; i++) {
				obj.ids.push(obj.question[i].id);
				// 分数
				if( !result[obj.question[i].id] ) result[obj.question[i].id] = "N/A";
				obj.question[i].score = result[obj.question[i].id];
				// 答案
				for (var j = 0; j < obj.question[i].option.length; j++) {
					obj.question[i].option[j].id = obj.question[i].id;
				}
				obj.question[i].option = JSON.stringify(obj.question[i].option);
			}
			obj.ids = JSON.stringify(obj.ids);
			switch(key){
				case "行为能力":
					obj.type = "behaviorAbility"; 
					arr[0] = obj;
					break;
				case "被动肌张力":
					obj.type = "passiveMuscle"; 
					arr[1] = obj;
					break;
				case "主动肌张力":
					obj.type = "activeMuscles"; 
					arr[2] = obj;
					break;
				case "原始反射":
					obj.type = "originalReflection"; 
					arr[3] = obj;
					break;
				case "一般反应":
					obj.type = "generalResponse"; 
					arr[4] = obj;
					break;
			}
		}
		return arr;
	}

	/**
	 * 获取初始结果(全部赋值为N/A)
	 */
	servers.getInitResult = function(result,questions){
		for (var i = 0; i < questions.length; i++) {
			for(var j = 0; j < questions[i].question.length; j++) {
				questions[i].question[j].score = "N/A";
				result[questions[i].question[j].id] = "N/A";
			}
		}
		return result;
	}

	/**
	 * 总分计算
	 */
    servers.countTotalScore = function(result){
        var score = 0, length = 0;
        for(var id in result){
            var val = result[id];
            if(val == "N/A"){
                length++;
            }else{
                score += val*1;
            }
        }
        $("#completion").html(score + "分 + " + length + "N/A");
    };

    /**
     * 判断当前能区是否含有"N/A"
     * 有：返回false；没有返回true；
     */
    servers.isHasNA = function(result,idS){
        var flag = true;
        for(var i = 0; i < idS.length; i++){
            if( result[idS[i]] == "N/A" ){
                flag = false;
                break;
            }
        }
        return flag;
    };

    /**
     * 获取下一个能区的索引号
     */
    servers.jumpNext = function(name,questions){
    	var len = questions.length;
    	for (var i = 0; i < len; i++) {
    		if( questions[i].name == name ){
    			return ( i+1 < len ) ? i+1 : -1;
    		}
    	}
    };

    /**
     * 各能区分数计算
     */
    servers.countScore = function(result){
        var score = {
        	"behaviorAbility":0,
        	"passiveMuscle":0,
        	"activeMuscles":0,
        	"originalReflection":0,
        	"generalResponse":0
        };
        for(var id in result){
            var val = result[id];
            if( val == "N/A" ) val = 0;
            if( id=='1'||id=='2'||id=='3'||id=='4'||id=='5'||id=='6' )  score.behaviorAbility += val*1.0;
            if( id=='7'||id=='8'||id=='9'||id=='10' ) 				    score.passiveMuscle += val*1.0;
            if( id=='11'||id=='12'||id=='13'||id=='14' ) 				score.activeMuscles += val*1.0;
            if( id=='15'||id=='16'||id=='17' ) 						    score.originalReflection += val*1.0;
            if( id=='18'||id=='19'||id=='20' ) 						 	score.generalResponse += val*1.0;
        }
        return score;
    };

    /**
     * 获取没有做的题的id的集合
     */
    servers.getNotDoQuestId = function(result){
        var notDo = [];
        for(var key in result){
            if(result[key] == "N/A"){
                notDo.push(key);
            }
        }
        return notDo;
    };

    /**
	 * 评估提交
	 */
	servers.ajaxSubmitGetScore = function(params,callback){
		$bb.ajax({
			url: URL + "service/evaluation/submitGetScore",
			data:JSON.stringify({
				data : params
			}),
			success: function(data){
				if(data.success){
					callback(data.data);
				}else{
					$bb.toastShow(data.msg ? data.msg : "评估提交出错！");
				}
			}
		});
	}

	return servers;
});